<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>进货商品管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/resources/css/public.css" media="all" />
</head>
<body class="childrenBody">
<!-- 查询条件开始 -->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 5px;">
    <legend>查询条件</legend>
</fieldset>
<blockquote class="layui-elem-quote">
    <form action="" method="post" id="searchFrm" lay-filter="searchFrm" class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">供应商名称</label>
                <div class="layui-input-inline">
                    <select name="providerid" id="search_providerid">
                        <option value="">请选择供应商</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-inline">
                    <select name="goodsid" id="search_goodsid">
                        <option value="">请选择商品</option>
                    </select>
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">开始时间</label>
                <div class="layui-input-inline">
                    <input type="tel" name="startTime" id="startTime" readonly="readonly" placeholder="yyyy-MM-dd HH:mm:ss" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">结束时间</label>
                <div class="layui-input-inline">
                    <input type="tel" name="endTime"  id="endTime"  readonly="readonly" placeholder="yyyy-MM-dd HH:mm:ss" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">操作员</label>
                <div class="layui-input-inline">
                    <input type="text" name="operateperson"   autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline" style="text-align: center;">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="doSearch"><span class="layui-icon layui-icon-search"></span>查询</button>
                    <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
                </div>
            </div>
        </div>
    </form>
</blockquote>
<!-- 查询条件结束-->
<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="inportTable" lay-filter="inportTable"></table>
    <div id="inportToolBar" style="display: none;">
        <button type="button" lay-event="add" class="layui-btn layui-btn-sm layui-btn"><span class="layui-icon layui-icon-addition"></span>添加</button>
    </div>
    <div id="inportRowBar" style="display: none;">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm layui-btn"><span class="layui-icon layui-icon-edit"></span>更新</button>
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>删除</button>
        <button type="button" lay-event="backGoods" class="layui-btn layui-btn-sm layui-btn-warm"><span class="layui-icon layui-icon-edit"></span>退货</button>
    </div>
</div>
<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 10px" id="addOrUpdateDiv">
    <form action="" method="post" class="layui-form layui-form-pane" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">供应商</label>
                <div class="layui-input-inline">
                    <select name="providerid" id="providerid"   lay-filter="providerid">
                        <option value="0">请选择供应商</option>
                    </select>
                    <div class="mydiv" title="不可修改" style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"></div>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-inline">
                    <select name="goodsid" id="goodsid">
                        <option value="0">请选择商品</option>
                    </select>
                    <div class="mydiv" title="不可修改" style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"> </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">进货数量</label>
                    <div class="layui-input-inline">
                        <input type="hidden" name="id">
                        <input type="text" class="layui-input"  name="number" lay-verify="number" placeholder="请输入商品进货包装">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">进货价格</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="inportprice" lay-verify="number" placeholder="请输入商品进货规格">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">支付类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="paytype" value="支付宝" title="支付宝">
                    <input type="radio" name="paytype" value="微信" title="微信">
                    <input type="radio" name="paytype" value="信用卡" title="信用卡">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">进货备注</label>
                <div class="layui-input-block">
                    <textarea  name="remark" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block" style="text-align: center;">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="doSubmit" id="doSubmit" ><span class="layui-icon layui-icon-add-1"></span>提交</button>
                    <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
                </div>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->

<!--退货弹出层开始-->
<div id="backGoodsDiv" style="display: none">
    <form action="" method="post" class="layui-form layui-form-pane" id="dataBackGoodsFrm" lay-filter="dataBackGoodsFrm">
        <div class="layui-form-item">
            <div class="layui-form-item">
                <label class="layui-form-label">进货数量</label>
                <div class="layui-input-block">
                    <input type="hidden" name="id"><!--进货单id-->
                    <input type="hidden" name="currentNumber" id="currentNumber"><!--当前进货单数量-->
                    <input type="text" class="layui-input"  name="number" lay-verify="required|checknumber" placeholder="请输入退货商品数量">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">进货原因</label>
                <div class="layui-input-block">
                    <textarea  name="remark" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block" style="text-align: center;">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="doSubmitBackGoods" id="doSubmitBackGoods" ><span class="layui-icon layui-icon-add-1"></span>提交</button>
                    <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
                </div>
            </div>
        </div>
    </form>
</div>
<!--退货弹出层结束-->

<script type="text/javascript" src="/resources/layui/layui.js"></script>
</body>
</html>
<script>
    layui.use(['jquery','form','table','layer','laydate'],function () {
        var $=layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var laydate = layui.laydate;

        //渲染时间选择器
        laydate.render({
            elem:"#startTime",
            type:"datetime"
        });
        laydate.render({
            elem:"#endTime",
            type:"datetime"
        });

        //加载表格数据
        var tableIns = table.render({
            elem: '#inportTable'
            ,url:'/inport/loadAllInport'
            ,toolbar: '#inportToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,title: '进货商品数据表'
            ,height:'full-180'
            ,page: true
            ,cols: [ [
                {field:'id', title:'进货ID',align:'center' ,width:'80'}
                ,{field:'providername', title:'供应商',align:'center',width:'180'}
                ,{field:'goodsname', title:'商品名称',align:'center',width:'150'}
                ,{field:'paytype', title:'支付类型',align:'center',width:'100'}
                ,{field:'inporttime', title:'进货时间',align:'center',width:'180'}
                ,{field:'operateperson', title:'操作员',align:'center',width:'100'}
                ,{field:'number', title:'进货数量',align:'center',width:'100'}
                ,{field:'size', title:'进货规格',align:'center',width:'100'}
                ,{field:'inportprice', title:'进货价格',align:'center',width:'120'}
                ,{field:'remark', title:'备注',align:'center',width:'200'}
                ,{fixed: 'right', title:'操作', toolbar: '#inportRowBar',align:'center',width:'250'}
            ] ]
            //数据渲染完的回调
            ,done: function(res, curr, count){ //处理删除某一页最后一条数据返回到上一页
                if(res.data.length==0 && curr!=1){
                    tableIns.reload({
                        page:{
                            curr:(curr-1)
                        }
                    });
                }
            }
        });

        //模糊查询
        form.on("submit(doSearch)",function(data){
            tableIns.reload({
                where:data.field,
                page:{
                    curr:1
                }
            });
            return false;
        });

        //监听头工具栏事件
        table.on("toolbar(inportTable)",function(obj){
            switch(obj.event){
                case 'add':
                    openAddLayer();
                    break;
            };
        });
        //监听行工具条的事件
        table.on("tool(inportTable)",function(obj){
            var data = obj.data; //获得当前行数据
            switch(obj.event){
                case 'delete':
                    deleteInport(data);
                    break;
                case 'update':
                    openUpdateInportLayer(data);
                    break;
                case 'backGoods':
                    backGoods(data);
                    break;
            };
        });

        var mainIndex;
        var url;
        //打开添加的弹出层
        function openAddLayer(){
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['800px','550px'],
                title:'添加进货商品',
                success:function(){
                    $("#dataFrm")[0].reset();
                    //重置下拉菜单
                    var provid = $("#providerid");
                    var html1 = '<option value="">请选择供应商</option>';
                    provid.html(html1);
                    form.render("select");
                    var goods = $("#goodsid");
                    var html = '<option value="">请选择商品</option>';
                    goods.html(html);
                    form.render("select");
                    url="/inport/addInport";
                    initProviderSelect();
                    $(".mydiv").hide();
                }
            });
        }
        //打开修改的弹出层
        function openUpdateInportLayer(data){
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['800px','500px'],
                title:'修改进货商品',
                success:function(){
                    $("#dataFrm")[0].reset();
                    //装载新的数据
                    form.val("dataFrm",data);
                    url="/inport/updateInport";
                    //加载查询条件供应商的下拉列表
                    initProviderSelect(data.providerid);
                    initGoodsSelect(data.providerid,data.goodsid)
                    $(".mydiv").show();
                }
            });
        }
        //初始化添加或修改供应商的下拉列表
        function initProviderSelect(providerid){
            $.get("/provider/loadAllProviderForSelect",function (res) {
                var redata = res.data;
                var dom = $("#providerid");
                var html = '<option value="">请选择供应商</option>';
                $.each(redata,function (index,item) {
                    html+='<option value="'+item.id+'">'+item.providername+'</option>';
                });
                dom.html(html);
                //修改用 如果providerid有值就反选
                if(providerid!=undefined){
                    dom.val(providerid);
                }
                form.render("select")
            });
        }
        //监听添加页面的供应商下拉框的改变事件
        form.on("select(providerid)",function (res) {
            var providerid = res.value;
            initGoodsSelect(providerid);
        })

        //初始化商品下拉列表
        function initGoodsSelect(providerid,goodsid){
            $.get("/goods/loadGoodsByProviderId",{providerid:providerid},function (res) {
                var data = res.data;
                var dom = $("#goodsid");
                var html = '<option value="">请选择商品</option>';
                $.each(data,function (index,item) {
                    html+='<option value="'+item.id+'">'+item.goodsname+'【'+item.providername+'】'+'</option>';
                });
                dom.html(html);
                //修改用 如果goodsid有值就反选
                if(goodsid!=undefined){
                    dom.val(goodsid);
                }
                form.render("select")
            })
        }

        form.on("submit(doSubmit)",function (data) {
            $.post(url,data.field,function(res){
                if(res.code==200){
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            })
        });

        //删除
        function deleteInport(data){
            layer.confirm('你确定要删除这条进货商品信息吗?', {icon: 3, title:'提示'}, function(index){
                $.post("/inport/deleteInport",{id:data.id},function(res){
                    if(res.code==200){
                        layer.msg(res.msg);
                        tableIns.reload();
                    }
                    layer.msg(res.msg);
                });
                layer.close(index);
            });
        }
        //加载查询条件供应商的下拉列表
        $.get("/provider/loadAllProviderForSelect",function (res) {
            var data = res.data;
            var dom = $("#search_providerid")
            var html = '<option value="">请选择供应商</option>';
            $.each(data,function (index,item) {
                html+='<option value="'+item.id+'">'+item.providername+'</option>';
            });
            dom.html(html);
            form.render("select")
        })
        //加载查询条件商品的下拉列表
        $.get("/goods/loadAllGoodsForSelect",function (res) {
            var data = res.data;
            var dom = $("#search_goodsid")
            var html = '<option value="">请选择商品</option>';
            $.each(data,function (index,item) {
                html+='<option value="'+item.id+'">'+item.goodsname+'【'+item.providername+'】'+'</option>';
            });
            dom.html(html);
            form.render("select")
        })

        //打开退货的弹出层
        function backGoods(data){
            mainIndex=layer.open({
                type:1,
                content:$("#backGoodsDiv"),
                area:['800px','550px'],
                title:'商品退货',
                success:function(){
                    $("#dataBackGoodsFrm")[0].reset();
                    //装载数据
                    form.val("dataBackGoodsFrm",{id:data.id,
                        currentNumber:data.number});
                }
            });
        }
        //自定义验证
        form.verify({
            checknumber: function (value) { //输入的值
                var currentNumber = parseInt($("#currentNumber").val());
                if(parseInt(value) > currentNumber || parseInt(value) < 1){
                    return '退货数量只能在【0】-【'+currentNumber+'】之间';
                }
            }
        });

        //提交退货单
        form.on("submit(doSubmitBackGoods)",function (data) {
            $.post('/outport/addOutport',data.field,function (res) {
                if(res.code == 200){
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex)
            });
        });
    })
</script>
